package com.dog.modules.system.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.framework.core.utils.BeanCopyUtils;
import com.dog.framework.core.utils.JsonUtils;
import com.dog.framework.log.annotation.OperateLog;
import com.dog.framework.log.enums.BusinessType;
import com.dog.framework.security.annotation.RepeatSubmit;
import com.dog.modules.system.api.dto.request.SysOperLogAddReqDto;
import com.dog.modules.system.api.dto.request.SysOperLogPageQueryDto;
import com.dog.modules.system.api.dto.response.SysOperLogRespVO;
import com.dog.modules.system.domain.SysOperLog;
import com.dog.modules.system.service.SysOperLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户信息
 *
 * @author liuxk
 */
@Slf4j
@RestController
@RequestMapping("/log")
public class SysOperLogController {


    @Autowired
    private SysOperLogService sysOperLogService;

    @SneakyThrows
    @PostMapping("/add")
    public Boolean add(@RequestBody SysOperLogAddReqDto dto) {
        SysOperLog sysOperLog = BeanCopyUtils.copy(dto,SysOperLog.class);
        return sysOperLogService.save(sysOperLog);
    }

    @OperateLog(title = "日志管理", businessType = BusinessType.OTHER)
    @RepeatSubmit
    @PostMapping("/list")
    public List<SysOperLog> list() {
        List<SysOperLog> sysOperLogList = sysOperLogService.list();
        return sysOperLogList;
    }


    @PostMapping("/page")
    public PageInfo<SysOperLogRespVO> page(@RequestBody SysOperLogPageQueryDto query) {
        PageInfo<SysOperLogRespVO> respVOPageInfo = new PageInfo<>();

        LambdaQueryWrapper<SysOperLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getStatus() != null, SysOperLog::getStatus, query.getStatus());
        wrapper.like(StrUtil.isNotBlank(query.getOperName()), SysOperLog::getOperName, query.getOperName());
        wrapper.like(StrUtil.isNotBlank(query.getTitle()), SysOperLog::getTitle, query.getTitle());
        wrapper.like(StrUtil.isNotBlank(query.getOperUrl()), SysOperLog::getOperUrl, query.getOperUrl());
        wrapper.orderByDesc(SysOperLog::getId);

        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<SysOperLog> operLogList = sysOperLogService.list(wrapper);
        PageInfo<SysOperLog> pageInfo = new PageInfo<>(operLogList);
        BeanUtil.copyProperties(pageInfo,respVOPageInfo);
        List<SysOperLogRespVO> respVOList = BeanUtil.copyToList(pageInfo.getList(),SysOperLogRespVO.class);
        respVOPageInfo.setList(respVOList);
        log.info("返回参数 ：{}",JsonUtils.toJsonString(respVOPageInfo));
        return respVOPageInfo;
    }




}
